<?php

declare(strict_types=1);

namespace App\Controller\Home\User;

use App\Model\Order;
use App\Model\Service;
use App\Services\BspayService;
use BsPaySdk\core\BsPay;
use BsPaySdk\core\BsPayClient;
use Hyperf\Redis\Redis;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;


class UserServiceController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Service $service;



    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request):ResponseInterface
    {
        $data['list'] = $this->service->orderBy('days')
            ->get()->toArray();
        $data['total']= $this->service->count();
        return $this->success($data);
    }

    public function testSdk(RequestInterface $request)
    {
        $number = $request->input('number');
        $order = Order::where('number', $number)->first();
        // 检查类是否能正常加载
       $service =  new BspayService();
       $result = $service->createPayment($order);
       $data =  [
           'success' => true,
           'result' => $result,
           'debug' => $this->debugSdk()
       ];
       return $this->success($data);
    }

    public function testRefund(RequestInterface $request){
        $number = $request->input('number');
        $fee = $request->input('fee');
        $order = Order::where('number', $number)->first();
        $order->refundOrder($fee);
        return $this->success();
    }

    /**
     * 调试 SDK 配置
     */
    public function debugSdk()
    {
        try {
            $config = BsPay::getConfig();
            return $config;
        } catch (\Exception $e) {
            return [
                'error' => $e->getMessage()
            ];
        }
    }



    function getExtendInfos() {
        // 设置非必填字段
        $extendInfoMap = array();
        // 账户号
        // $extendInfoMap["acct_id"]= "";
        // 交易有效期
//        $extendInfoMap["time_expire"]= "20250518235959";
        // 微信参数集合
        $extendInfoMap["wx_data"]= get168591ab37ca4acb897b19582b0f73ba();
        // 支付宝参数集合
        $extendInfoMap["alipay_data"]= getDc341529804741c19fd28b3f6e9b3cce();
        // 银联参数集合
        $extendInfoMap["unionpay_data"]= get8522599bC4cd433686a4000295dc2c07();
        // 数字人民币参数集合
        // $extendInfoMap["dc_data"]= get4cbfb50f28374e99A5f17b6680a8ba8f();
        // 是否延迟交易
        $extendInfoMap["delay_acct_flag"]= "N";
        // 手续费扣款标志
        // $extendInfoMap["fee_flag"]= "";
        // 分账对象
        $extendInfoMap["acct_split_bunch"]= getA8c3340fD7ad4be88eb3Bbd621899bc5();
        // 传入分账遇到优惠的处理规则
        $extendInfoMap["term_div_coupon_type"]= "0";
        // 补贴支付信息
        // $extendInfoMap["combinedpay_data"]= getA7ba95817bd540a9Be240bfc181ed1a5();
        // 禁用信用卡标记
        $extendInfoMap["limit_pay_type"]= "NO_CREDIT";
        // 商户贴息标记
        $extendInfoMap["fq_mer_discount_flag"]= "N";
        // 渠道号
        $extendInfoMap["channel_no"]= "";
        // 场景类型
        $extendInfoMap["pay_scene"]= "02";
        // 备注
        $extendInfoMap["remark"]= "string";
        // 安全信息
        $extendInfoMap["risk_check_data"]= get5c4e203a5bd643ff9afc6140d6b2f5ea();
        // 设备信息
        $extendInfoMap["terminal_device_data"]= get5269ec51D37a4b589f4a453164d6ff69();
        // 异步通知地址
        $extendInfoMap["notify_url"]= "http://www.baidu.com";
        // 手续费补贴信息
        // $extendInfoMap["trans_fee_allowance_info"]= getC2b2fc1aAfc843de93249614a637160e();
        return $extendInfoMap;
    }

}
